** Clean announcement data
** Combines NSW 06-13, VZ 14-15 and hand collected data
** JHL

*************************************
** Set up workspace
*************************************
version 14.0
clear all
set more off

cd "${path_home}"
** log using "${path_log}/c01_mw_enact_announce_vz", text replace

adopath + ../programs
ssc install blindschemes, replace all
ssc install univar 

use "${path_big_dta}/mw/announcement/announcement_0615_quarterly", clear 

**************************************
** [1] Understand events 
**************************************

** Transform dates, create lag b/w implement and announcement 
cap drop yq
gen yq = yq(year,quarter)
format yq %tq
order yq, after(quarter)

cap drop source

** Generate implementation and announcement dates, as well as lag 
gen ann_date = date(announcement_date, "MDY")
gen imp_date = date(implementation_date, "MDY")
format ann_date %td
format imp_date %td 
gen imp_ann_lag = imp_date - ann_date

** Quarterly lag b/w implementation and announcement 
gen imp_ann_q = imp_ann / 91

** Event ids
egen event_id = group(imp_date state)
	* 222 events (179 from 2006-2013, 43 from 2014-2015)
	distinct event_id

bys event_id (yq): gen event_first = _n == 1
	
** Announcement ids 
egen ann_id = group(ann_date state) 

bys ann_id (yq): gen ann_first = _n == 1

sort state yq 

gen ann_year = year(ann_date)
gen ann_quarter = quarter(ann_date)
gen ann_month = month(ann_date)
gen ann_yq = yq(ann_year,ann_quarter)
gen ann_ym = ym(ann_year, ann_month)
format ann_yq %tq
format ann_ym %tm

** Number of events in each state
bys state: egen total_events = total(event_first)
bys state (yq): gen state_ind = _n == 1

** Date of events, by type of change
gen fed_leg = event_first if type == "legislation" & fed == 1 	
gen state_leg = event_first if type == "legislation" & fed == 0 
gen ballot = event_first if type == "ballot"
gen index = event_first if type == "indexation"

save "${path_big_dta}/mw/announcement/ann_edited_0615", replace 

** TABLE IN PAPER *** 
** MW changes by year and type 
** graph bar (sum) fed_leg state_leg ballot index, over(year) stack legend(lab(1 "Federal Legislation") lab(2 "State Legislation") lab(3 "State Ballot") lab(4 "State Indexation")) graphregion(color(white)) scheme(plottig)
** translate @Graph "${path_out}/graphs/mw_changes_year_type.pdf", name("Graph") replace

** Black and white 
** graph bar (sum) fed_leg state_leg ballot index, over(year) stack legend(lab(1 "Federal Legislation") lab(2 "State Legislation") lab(3 "State Ballot") lab(4 "State Indexation")) graphregion(color(white)) scheme(s2mono)
** translate @Graph "${path_out}/graphs/mw_changes_year_type_bw.pdf", name("Graph") replace

****************************************************************************
** [3] Create quarterly panel of MW with both announced and actual MW 
****************************************************************************

** [3.1] Prepare announcement MW data 

use "${path_big_dta}/mw/announcement/ann_edited_0615", clear 

** Take care of duplicated events (events that happened within quarter) 
sort state ann_date imp_date yq

bys state imp_date (yq): gen count = _n
bys state imp_date (yq): gen max_count = _N 

keep if count==max_count
drop count max_count 

keep state year quarter yq ann_date imp_date type

save "${path_big_dta}/mw/announcement/ann_mw_merge_0615", replace  


** [3.2] Merge with daily data, then collapse to quarterly with mean across days 
use "${path_big_dta}/mw/VZ/VZ_state_stata/VZ_state_daily", clear 

gen state = statefips
gen imp_date = date 
format imp_date %td

merge m:1 state imp_date using "${path_big_dta}/mw/announcement/ann_mw_merge_0615", keep(master match) nogen 

** Remember to allow updating 
merge m:1 statefips imp_date using "${path_big_dta}/mw/announcement/announcement_040516_changes", keep(master match match_update match_conflict) update nogen 

gen ann_mw = mw 
** Keep start and end dates as well as announcement dates 
keep if (ann_date!=.|date==mdy(1,1,2004)|date==mdy(7,1,2016))

** Collapse if announcement contains multiple MW changes, keep start and end dates intact) 
bys state ann_date (imp_date): gen count = _n  
egen max_mw = max(mw) if ann_date!=., by(state ann_date)
replace ann_mw = max_mw if ann_date!=.
keep if (count==1|date==mdy(1,1,2004)|date==mdy(7,1,2016)) 
drop count max_mw 

replace ann_date = date if date==mdy(1,1,2004)|date==mdy(7,1,2016)

xtset state ann_date 
tsfill 

replace ann_mw = ann_mw[_n-1] if ann_mw == .

drop mw date statefips
gen statefips = state
gen date = ann_date 
format date %td 
merge m:1 statefips date using "${path_big_dta}/mw/VZ/VZ_state_stata/VZ_state_daily", keep(master match) nogen 

replace mw = mw[_n-1] if mw == .

drop year quarter yq 
gen year = year(ann_date)
gen quarter = quarter(ann_date)
cap noi drop month 
gen month = month(ann_date) 
gen yq=yq(year,quarter) 
format yq %tq 
gen ym=ym(year,month) 
format ym %tm 

collapse mw ann_mw, by(state year quarter yq)
rename mw mean_mw 

gen fips_state_code = state 

save "${path_big_dta}/mw/announcement/announcement_0615_quarterly_panel", replace 

****************************************************************************
** [4] Generate panel of MW with leads and lags
****************************************************************************

** Quarterly 
use "${path_big_dta}/mw/VZ/VZ_state_stata/VZ_state_quarterly.dta", clear
gen yq = quarterly_date
format yq %tq 
gen state = statefips 
merge m:1 state yq using "${path_big_dta}/mw/announcement/announcement_0615_quarterly_panel", keep(master match) nogen 

tsset statefips quarterly_date
gen log_mwq=log(mean_mw) 	
gen log_ann_mwq = log(ann_mw)
gen d1log_mwq = d1.log_mwq 
gen d1log_ann_mwq = d1.log_ann_mwq 

foreach i of numlist 1/12 {
	foreach v of varlist log_mwq mean_mw log_ann_mwq d1log_mwq d1log_ann_mwq {
		gen f`i'`v' = f`i'.`v'
		gen l`i'`v' = l`i'.`v' 
		gen f`i's1`v' = f`i'.s1.`v'
		gen l`i's1`v' = l`i'.s1.`v'
	}
}

foreach v of varlist log_mwq mean_mw log_ann_mwq d1log_mwq d1log_ann_mwq {
	gen l0`v' = `v'
	gen l0s`v' = s1.`v'
}

save "${path_big_dta}/mw/VZ/VZ_state_stata/VZ_state_quarterly_fl.dta", replace 


*************************************
** Close workspace
*************************************
timer off 1
timer list 1 
** log close
